package org.lvhengchang.schoolmanager.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lvhengchang.schoolmanager.bean.Teacher;
import org.lvhengchang.schoolmanager.service.TeacherService;
import org.lvhengchang.schoolmanager.service.impl.TeacherServiceImpl;

/**
 * 教师修改操作处理类
 * 
 * @author LvHengchang
 *
 */
@WebServlet("/teacher/update")
public class TeacherUpdateServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private TeacherService teacherService = new TeacherServiceImpl();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String teacher_id = req.getParameter("teacher_id");

		try {
			Teacher teacher = teacherService.selectById(teacher_id);
			req.setAttribute("teacher", teacher);
			req.getRequestDispatcher("/teacherUpdate.jsp").forward(req, resp);
		} catch (Exception e) {
			throw new ServletException(e);
		}

	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");

		String teacher_name = (String) req.getParameter("teacher_name");
		String teacher_birthday = (String) req.getParameter("teacher_birthday");
		String sex = (String) req.getParameter("sex");
		String remarks = (String) req.getParameter("remarks");
		String teacher_id = req.getParameter("teacher_id");

		Teacher teacher = new Teacher();
		teacher.setTeacher_name(teacher_name);
		teacher.setTeacher_birthday(teacher_birthday);
		teacher.setSex(sex);
		teacher.setRemarks(remarks);
		teacher.setTeacher_id(teacher_id);

		try {
			teacherService.updateTeacher(teacher);
			req.setAttribute("teacher_id", teacher_id);
			req.setAttribute("teacher_name", teacher_name);
			req.setAttribute("teacher_birthday", teacher_birthday);
			req.setAttribute("sex", sex);
			req.setAttribute("remarks", remarks);
			req.getRequestDispatcher("/teacher/teacher_update_success.jsp").forward(req, resp);
		} catch (Exception e) {
			throw new ServletException(e);
		}

	}

}
